package me.msz.application.m2copy.core;

import ch.qos.logback.classic.LoggerContext;
import me.msz.application.m2copy.core.impl.DefaultCopyable;
import org.apache.commons.cli.ParseException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by MeiSZ on 2016/12/12.
 */
public class M2RepoCopier {
    private Logger logger = LoggerFactory.getLogger(M2RepoCopier.class);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

    private static M2RepoCopier ourInstance = new M2RepoCopier();

    public static M2RepoCopier getInstance() {
        return ourInstance;
    }

    private M2RepoCopier() {
    }

    public void doCopyRepo(boolean isCopyAll, boolean isOnlyJars, boolean isNeedCompress, boolean isVerbose,
                           String CompressAlgoName, String srcPath, String destPath)
            throws ParseException, IOException {
        File srcPathRoot = new File(srcPath);
        File destPathRoot = new File(destPath);
        if(destPathRoot.exists()) {
            logger.info("Destination directory " + destPathRoot.getAbsolutePath() + " is now existed.");
            throw new ParseException("Destination directory is now existed.");
        } else {
            if(isVerbose) {
                logger.info("Create destination directory " + destPathRoot.getAbsolutePath());
            } else {
                destPathRoot.mkdirs();
            }
        }

        if(srcPathRoot.exists()) {
            Iterator<File> pathIterator = FileUtils.iterateFiles(srcPathRoot, new String[]{"repositories", "jar", "pom", "sha1"}, true);
            while(pathIterator.hasNext()) {
                File fileInst = pathIterator.next();
                DefaultCopyable defaultCopyable = new DefaultCopyable(fileInst);

                if(isCopyAll) {
                    doCopy(isOnlyJars, isVerbose, srcPath, destPath, destPathRoot, fileInst);
                } else if(!(defaultCopyable.isCopied())) {
                    doCopy(isOnlyJars, isVerbose, srcPath, destPath, destPathRoot, fileInst);
                    defaultCopyable.setCopied();
                }

//                pathIterator.remove();
            }
        } else {
            throw new ParseException("Invalid argument: " + srcPath + ", the source directory is not existed.");
        }

    }

    private void doCopy(boolean isOnlyJars, boolean isVerbose, String srcPath, String destPath, File destPathRoot, File fileInst) throws IOException {
        if(isOnlyJars) {
            if(fileInst.getName().endsWith(".jar")) {
                if (isVerbose) {
                    logger.info("Copy file " + fileInst.getAbsolutePath() + " to " +
                            destPathRoot.getAbsolutePath());
                } else {
                    FileUtils.copyFileToDirectory(fileInst, destPathRoot);
                }
            }
        } else {
            String realDestPath = destPath + StringUtils.substring(fileInst.getParentFile().getAbsolutePath(), srcPath.length());
            if(isVerbose) {
                logger.info("Copy file " + fileInst.getAbsolutePath() + " to " + realDestPath);
            } else {
                File realDestDir = new File(realDestPath);
                realDestDir.mkdirs();

                FileUtils.copyFileToDirectory(fileInst, realDestDir);
            }
        }
    }
}
